package guru.merlin.core;

import guru.mercurio.common.PhoneAddress;
import guru.mercurio.common.RelativeApplicationAddress;

import org.apache.log4j.BasicConfigurator;



public class EPTester implements EnvelopeHandler{
    EnvelopeHandler sender;
    
    
	public static void main(String argv[]){
	    BasicConfigurator.configure();
	    
		EnvelopeProcessor ep;
		String className,propFilePath;
	    
	    
		if(argv.length<2){
			System.out.println("syntax: java guru.merlin.core.EPTester <EP class name> <Properties file path> [<ShorCode Fuente> <area destino> <numero destino> <texto a enviar>]");
		}
		else{
		    className=argv[0];
                    System.out.println("Class Name: " +className);
		    propFilePath=argv[1];
		    
			try{
				System.out.println("EPTester corriendo en modo 'echo'.");
				System.out.println("---------------------------------.");
				ep=(EnvelopeProcessor)Class.forName(className).newInstance();
				System.out.println("Envelope Processor instanciado.");
				ep.addEnvelopeHandler(new EPTester(ep));
				System.out.println("Enrutamiento de 'Echo' configurado");
				ep.openConnection(propFilePath);
				System.out.println("'openConnection' ejecutado");
				ep.start();
				System.out.println("'start' ejecutado.");

				if (argv.length>2){
				    System.out.println("argv.2_Puerto="+argv[2]); //Puerto
				    System.out.println("argv.3_AreaCode="+argv[3]); //AreaCode
				    System.out.println("argv.4_Local="+argv[4]); //Local
				    System.out.println("argv.5_Content="+argv[5]); //Content
                    System.out.println("argv.6_Operador="+argv[6]); //operador
				    
					System.out.println("EPTester corriendo en modo 'terminacin'.");
					System.out.println("---------------------------------.");
					
					//Envelope env=new Envelope(argv[5],new PhoneAddress("58",argv[3],argv[4]),new PhoneAddress("0","0",argv[2]));
                    Envelope env=new Envelope(argv[5],new PhoneAddress("58",argv[3],argv[4]),new RelativeApplicationAddress(argv[6],argv[2]));
					Thread.sleep(3000);
					
					ep.handEnvelope(env);
					System.out.println("'envelope' enviado");
				}
			}
			catch(Exception e){
				e.printStackTrace();
			}   
		}
	}

	public EPTester(EnvelopeHandler h){
	    sender=h;
	}

    /* (non-Javadoc)
     * @see guru.merlin.core.EnvelopeHandler#handEnvelope(guru.merlin.core.Envelope)
     */
    public Object handEnvelope(Envelope envelope) throws CantHandException {
        Address e;

		System.out.println("El EPTester ha recibido un mensaje...");

		if(envelope.wellFormed()){
		    e=envelope.getOrigin();
		    envelope.setOrigin(envelope.getDestination());
		    envelope.setDestination(e);
               
        //	envelope.setDestinationRoute(envelope.getDestination().toString());
               
		    sender.handEnvelope(envelope);
		    System.out.println("El EPTester ha contestado el mensaje.");
		}
		else{
			System.out.println("El mensaje no estaba bien formado. El EPTester ha rechazao el mensaje.");
		    
		}
		
        return envelope;
    }

    /* (sin Javadoc)
     * @see guru.merlin.core.EnvelopeHandler#retrieveEnvelope(java.lang.Object)
     */
    public void retrieveEnvelope(Object EnvelopeReference) throws CantHandException {
    }

    /* (sin Javadoc)
     * @see guru.merlin.core.EnvelopeHandler#commitHandEnvelope(java.lang.Object)
     */
    public void commitHandEnvelope(Object EnvelopeReference) throws CantHandException {
    }


}

